<script type="text/javascript">
  RED.nodes.registerType('ads-client-convert-to-raw', {
    paletteLabel: 'ADS - Convert To Raw',
    category: 'TwinCAT ADS',
    icon: 'font-awesome/fa-exchange',
    align: 'right',
    color: '#3FADB5',
    inputs: 1,
    inputLabels: function () {
      return this.dataTypeName === '' ? "Value (topic as data type)" : "Value"
    },
    outputs: 1,
    outputLabels: "Data",
    defaults: { 
      name: { value: '' },
      connection: { value: null, type: "ads-client-connection" },
      dataTypeName: { value: "", required: false },
      autoFill: { value: false }
    },
    label: function () {
      if (this.connection === null)
        return `${(this.name || `ADS - Convert To Raw`)} (*Not configured*)`;

      if (this.name) {
        return this.name;
      }

      return `ADS - Convert To Raw (${(this.dataTypeName === '' ? `msg.topic` : `${this.dataTypeName}`)})`;
    },
    oneditprepare: function () {
      //Stuff that is done when properties panel is opened
    }
  })
</script>



<!-- Properties -->
<script type="text/html" data-template-name="ads-client-convert-to-raw">
  <div class="form-row">
      <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
      <input type="text" id="node-input-name" placeholder="Name (optional)">
  </div>

  <div class="form-row">
      <label for="node-input-connection"><i class="fa fa-bookmark"></i> ADS connection</label>
      <input type="text" id="node-input-connection" placeholder="ADS Connection">
  </div>

  <div class="form-row">
      <label for="node-input-dataTypeName"><i class="fa fa-tag"></i> Data type</label>
      <input type="text" id="node-input-dataTypeName" placeholder="Data type">
  </div>
  <div class="form-tips"><b>Tip:</b> Leave data type empty to use <code>msg.topic</code> input instead.</div>
  
  <div class="form-row">
    <!-- Some margin after tip -->
  </div>

  <div class="form-row">
    <input type="checkbox" id="node-input-autoFill" style="display: inline-block; width: auto; vertical-align: top;">
    <label for="node-input-autoFill" style="width: auto"> Automatically fill missing properties <b>with default empty values</b> (<a href="https://github.com/jisotalo/ads-client#converting-a-javascript-object-to-raw-value" target="_blank"
      style="text-decoration: underline"><code>autoFill</code></a>)</label>
  </div>
</script>




<!-- Help -->
<script type="text/html" data-help-name="ads-client-convert-to-raw">
<p>
  Converts given Javascript object/variable to raw Buffer data by given data type (like REAL, ST_Struct).
</p>
<p>
  The input <code>msg.payload</code> is the value to be converted, its type depends to PLC variable type.
  If input <code>msg.topic</code> is given, the topic is used as data type if no name is given in properties.
</p>


<h3>Properties (settings)</h3>
  <dl class="message-properties">
    <dt>Name
      <span class="property-type">string</span>
    </dt>
    <dd> Optional node name</dd>
  </dl>
  
  <dl class="message-properties">
    <dt>ADS connection
      <span class="property-type">ads-client-connection</span>
    </dt>
    <dd> ADS client connection (target system) to use</dd>
  </dl>

  <dl class="message-properties">
    <dt>Data type
      <span class="property-type">string</span>
    </dt>
    <dd> Data type in the PLC - Example: <code>ST_SomeStruct</code>, <code>REAL</code>,..</dd>
  </dl>

  <dl class="message-properties">
    <dt>Automatically fill missing properties
      <span class="property-type">bool</span>
    </dt>
    <dd> If data type is structured (STRUCT, FB..), any missing properties are written to default values (usually 0).</dd>
  </dl>


<h3>Inputs</h3>
  <dl class="message-properties">
    <dt>payload
      <span class="property-type">any</span>
    </dt>
    <dd> The value to convert to raw Buffer object</dd>
  </dl>
  <dl class="message-properties">
    <dt>topic
      <span class="property-type">undefined | string</span>
    </dt>
    <dd> If given and no data type set in properties, this topic is used as data type.</code></dd>
  </dl>

<h3>Outputs</h3>
  <dl class="message-properties">
    <dt>payload <span class="property-type">Buffer</span></dt>
    <dd>The value as raw Buffer data</dd>
    <dt>... <span class="property-type">any</span></dt>
    <dd>All input <code>msg</code> properties are forwarded</dd>
  </dl>

<h3>Details</h3>
  <p>
  The input <code>msg.payload</code> should match the PLC variable type. For example, if PLC type is <code>STRING</code>, the payload should be value that can be handled as string.
  And if the data type is STRUCT, the payload should be a JavaScript object matching the PLC struct (JSON).
  </p>
  <p>
  If <code>autoFill</code> is set, the given JSON doesn't need to include all structured data type properties. Please see 
  <a href="https://github.com/jisotalo/ads-client#converting-a-javascript-object-to-raw-value"
    target="_blank" style="text-decoration: underline">
    ads-client README
  </a>.
  </p>
  <p>
  If the data type is not set in properties, the input <code>msg.topic</code> is used as name instead.
  </p>

<h3>References</h3>
  <ul>
    <li><a href="https://github.com/jisotalo/ads-client#converting-a-javascript-object-to-raw-value" target="_blank" style="text-decoration: underline">
      ads-client convertToRaw() readme
    </a></li>
    <li><a href="https://jisotalo.github.io/ads-client/Client.html#convertToRaw" target="_blank" style="text-decoration: underline">
      ads-client convertToRaw() documentation
    </a></li>
  </ul>
</script>